<!--决算单列表-->
<template>
  <div class="acct-balance-bill ne-relative">
    <div style="height: 100%;overflow-y: auto;">
      <DicTable
          height="100%"
          stripe
          highlight-current-row
          highlight-hover-row
          max-height="600"
          ref="GBLtable"
          border
          header-row-class-name="header-row-class"
          row-class-name="ne-row-class"
          :columnData="GBLtbs.tableColumns"
          :tableData="GBLtbs.tableData"
          :loading="GBLtbs.tableLoading"
          :currentPage="~~GBLtbs.tablePage.pageIndex"
          :pageSize="~~GBLtbs.tablePage.pageSize"
          :totalResult="~~GBLtbs.tablePage.rowCount"
          :pager="true"
          @cell-click="cellClickEvent"
          @on-page-change="handlePageChange"
      >
        <template #custSoNumber="{}">
          <p>{{CTOInfo.custSoNumber + '-' + CTOInfo.batchNbr}}</p>
        </template>
      </DicTable>
      <el-dialog class="" append-to-body width="98%" top='3vh' title="决算单详情" v-if="dialogVisible"
                 :visible.sync="dialogVisible">
        <AcctBalanceInfo :show.sync="dialogVisible" :rowInfo="rowInfo"
                          :allInfo="{...CTOInfo,...ODKInfo}"></AcctBalanceInfo>
      </el-dialog>
      <el-row class="fixed">
        <el-col :span="24" class="ne-line-r flex flex-center">
            <el-button v-if="iftasks" type="primary" size="medium" @click="openDialog">送审核</el-button>
            <el-button v-if="iftasks" type="primary" size="medium" @click="confirm">确认</el-button>
            <el-button type="primary" size="medium" @click="reBalance">重新决算</el-button>
            <el-button size="medium" @click="closeFun">返回</el-button>
        </el-col>
      </el-row>
    </div>
    <el-dialog append-to-body title="选择审核人" v-if="AuditdialogVisible" :visible.sync="AuditdialogVisible">
      <el-form :model="audName">
        <el-form-item :label="audName.cName" label-width="1rem">
          <el-select v-model="audName.cValue" placeholder="请选择">
            <el-option
                v-for="(item,index) in audName.opt"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sendAuditSave">发送</el-button>
        <el-button @click="AuditdialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import AcctBalanceInfo from "@/components/custorder/pengding/AcctBalanceInfo";
import DicTable from "@/components/public/DicTable";
import { mapState } from "vuex";
import CustOrderService from "@/api/order/CustOrderService";
import refreshList from "@/mixins/custorder/refreshList";
export default {
  name: "AcctBalanceBill",
  mixins: [refreshList],
  data() {
    return {
      rowInfo: {},
      allInfo: {},
      dialogVisible: false,
      iftasks: false,
      GBLtbs: {
        tableColumns: [
          { field: "acctItemListId", title: "决算id"},
          { field: "custName", title: "客户名称", width: '16%'},
          { slotName: "custSoNumber", title: "流水号-批次号"},
          { field: "contactCd", title: "合同编码"},
          { field: "accountUnitName", title: "收款单位"},
          { field: "moneyRmb", title: "收费总额"},
          { field: "sendAudFlag", title: "送审标识"}
        ],
        tableData:  [],
        tableLoading: false,
        tablePage: {
          pageIndex: 1,
          pageSize: 10,
          rowCount: 0,
        },
      },
      audName: {
        type: 'select',
        cName: '审核人',
        cValue: '',
        required: true,
        opt: [],
        show: true,
        span: 24,
      },
      AuditdialogVisible:false,
      dialogTitle:''
    }
  },
  props: {
    ODKInfo: {
      type: Object
    },
    CTOInfo: {
      type: Object
    }
  },
  mounted() {

  },
  created() {
    this.getDetail();
  },
  computed:{
    ...mapState('user',['user'])
  },
  components: {DicTable,AcctBalanceInfo},
  methods: {
    delDHFun(str){
      let str2 = null
      if(str.substring(str.length-1,str.length)==","){
        str2=str.substring(0,str.length-1);
      }
      return str2;
    },
    async reBalance() {
      let {custSoNumber, custId,orderAuditId} = this.CTOInfo,
          acctItemListId = '';
      this.GBLtbs.tableData.map((item,index)=>{
        acctItemListId+=item.acctItemListId+',';
      });
      acctItemListId = this.delDHFun(acctItemListId);
      let params = {
        custId: custId,
        custSoNumber: custSoNumber,
        userId: this.user.staffId,
        orgId: this.user.orgId,
        userName: this.user.staffName,
        orderAuditId: orderAuditId,
        acctItemListId: acctItemListId,
        taskId :"TACHE00140002",
        oneOrmany:"many",
        reBalance:"1"  //重新结算标识
      }
      const result = await CustOrderService.reBalanceBill(params);
      if (result.meta.resultCode == 0) {
        this.$message({
          type: 'success',
          message: '保存成功!',
        });
        this.getDetail();
      } else {
        this.$message({
          showClose: true,
          message: result.meta.resultMsg,
          type: 'error',
        });
      }
    },
    openDialog(){
      this.AuditdialogVisible = true;
    },
    async sendAuditSave(){
      let{custOrderId,custId,batchNbr,custSoNumber,orderAuditId} = this.CTOInfo;
      let{taskId} = this.ODKInfo;
      let audUserId = this.audName.cValue,
          audUserName = this.getLabel(this.audName.opt,audUserId),
          oneOrmany = 'many',
          {acctItemListId} = this.GBLtbs.tableData[0],
          staffId = this.user.staffId,
          userId = this.user.staffId;
      const result = await CustOrderService.sendAudBalanceBill({orderAuditId,taskId,userId,batchNbr,acctItemListId,oneOrmany,staffId,custOrderId,custId,audUserId,audUserName,custSoNumber});
      if(result.meta.resultCode == 0){
        this.$message({
          type: 'success',
          message: '送审核成功!'
        });
        this.AuditdialogVisible=false;
        this.eventDoSearch();
        this.$emit('update:show',false);
      }else{
        this.$message({
          showClose: true,
          message: result.meta.resultMsg,
          type: 'error'
        });
      }
    },
    async confirm(){
      let{custOrderId,batchNbr,custId,custSoNumber,orderAuditId} = this.CTOInfo;
      let{taskId} = this.ODKInfo;
      let oneOrmany='many',
          staffId = this.user.staffId,
          userId = this.user.staffId,
          audFlag = 1;
      const result = await CustOrderService.audBalanceBill({orderAuditId,taskId,custSoNumber,userId,custOrderId,batchNbr,custId,oneOrmany,staffId,audFlag});
      if(result.meta.resultCode == 0){
        this.$message({
          type: 'success',
          message: '保存成功!'
        });
        this.eventDoSearch();
        this.$emit('update:show',false);
      }else{
        this.$message({
          showClose: true,
          message: result.meta.resultMsg,
          type: 'error'
        });
      }
    },
    async getAudUserList(){
      if(this.GBLtbs.tableData.length<=0) return
      let {custId,custOrderId,orderAuditId} = this.CTOInfo,
          {acctItemListId} = this.GBLtbs.tableData[0];
      const result = await CustOrderService.gryBalanceAudUserList({custId,custOrderId,acctItemListId,orderAuditId});
      this.audName.opt = {...result.meta};
    },
    async getDetail(){
      let{custSoNumber, orderAuditId, custOrderId,batchNbr,custId} = this.CTOInfo,
          pageInfo={
            pageIndex: ~~this.GBLtbs.tablePage.pageIndex+'',
            pageSize: ~~this.GBLtbs.tablePage.pageSize+''
          },
          params = {custOrderId,batchNbr,custId,pageInfo:pageInfo};
      const result = await CustOrderService.qryFinalAccountList(params);
      if(result.meta.resultCode=='0' || !result.meta.resultCode){
        this.GBLtbs.tableData = result.meta.acctItemList;
        this.GBLtbs.tablePage = {
          pageIndex: ~~Number(result.meta.pageInfo.pageIndex),
          pageSize: ~~Number(result.meta.pageInfo.pageSize),
          rowCount: ~~Number(result.meta.pageInfo.rowCount)
        };
        this.getAudUserList();
      }else{
        this.$message({
          showClose: true,
          message: result.meta.resultMsg,
          type: 'error'
        });
      };
      let usersMsg = {
            acceptUserId: this.user.staffId,
            acceptUserOrg: this.user.orgId,
            systemUserId: this.user.acctId,
            dealType: 234,
            custId: custId
          },
          allprms = { custSoNumber, orderAuditId, ...usersMsg };
      const result2 = await CustOrderService.queryCustOrderDealTask(allprms);
      if(result2.meta.resultCode == '0'){
        let tasks = result2.meta.orderDealTasks;
        tasks.forEach((item,index)=>{
          if(item.taskUrl == 'AcctBalanceBill'){
            this.iftasks = true;
          }
        });
      }else{
        this.$message({
          showClose: true,
          message: results2.meta.resultMsg,
          type: 'error'
        });
      }
    },
    getLabel(opt,VALUE){
      for(var key in opt){
        let item = opt[key];
        if(item.value==VALUE){
          
          return item.label;
        }
      }
    },
    handlePageChange({ currentPage, pageSize }) {
      this.GBLtbs.tablePage.pageIndex = currentPage;
      this.GBLtbs.tablePage.pageSize = pageSize;
      this.getDetail();
    },
    // 行点击事件
    cellClickEvent () {
      this.rowInfo = arguments[0].row;
      this.rowInfo.orderAuditId =this.CTOInfo.orderAuditId;
      this.dialogVisible = true;
    },
    closeFun(){
      this.$emit('update:show',false);
    },
  }
}
</script>

<style lang="scss" scoped>
.acct-balance-bill{
  height: 84vh;
  padding-bottom: .3rem;
  .fixed{
    position: absolute;
    bottom: -.1rem;
    left: 0;
    right: 0;
    margin: 0 auto;
  }
}
</style>
